#include "list.h"

void list_tail(struct dl_node *head, struct dl_node *what) 
{
    assert(head != NULL);
    assert(what != NULL);
    
    head->prev->next = what;
    what->prev = head->prev;
    head->prev = what;
    what->next = head;
} 

void list_append(struct dl_node *to, struct dl_node *what) 
{
    assert(to != NULL);
    assert(what != NULL);

    what->next = to->next;
    what->prev = to;
    to->next = what;
    what->next->prev = what;	/* FIXED: */
}

void list_prepend(struct dl_node *to, struct dl_node *what) 
{
    assert(to != NULL);
    assert(what != NULL);

    what->next = to;
    what->prev = to->prev;

    to->prev = what;
    what->prev->next = what;	/* FIXED: */
}

void list_init(struct dl_node *list) 
{
    assert(list != NULL);

    list->next = list->prev = list;
}

void list_remove(struct dl_node *node)
{
    assert(node != NULL);
    
    if (node->prev == node ||
        node->next == node)
        return ;
        
    node->prev->next = node->next;
    node->next->prev = node->prev;
	node->next = node->prev = node;
}

